#!/bin/bash


shellout(){
  echo "$1";
  exit 1;
}

VERSION=`tail -1 ../src/VERSION`


current_user=`id`
if ! echo $current_user | grep -q "uid=0"
then
  echo "You must be root !"
  echo "Try sudo $0"
  exit 3
fi

bash ./build.sh || shellout "Error during build.sh command"
ARCH=`arch`
if ! echo "$ARCH" | grep -q "64"
then
ARCH="i386"
else
ARCH="amd64"
fi

echo
echo "ARCH=$ARCH"
echo

apt-get install build-essential qt4-dev-tools

sed -i "s/Architecture: .*$/Architecture: $ARCH/g" deb-package/DEBIAN/control
sed -i "s/Version: .*$/Version: $VERSION/g" deb-package/DEBIAN/control

mkdir -p deb-package
mkdir -p deb-package/DEBIAN
mkdir -p deb-package/etc
mkdir -p deb-package/etc/kakola
mkdir -p deb-package/etc/kakola/profiles
mkdir -p deb-package/usr
mkdir -p deb-package/usr/bin
mkdir -p deb-package/usr/share
mkdir -p deb-package/usr/share/applications
mkdir -p deb-package/usr/share/doc
mkdir -p deb-package/usr/share/doc/kakola
mkdir -p deb-package/usr/share/icons
mkdir -p deb-package/usr/share/icons/hicolor
mkdir -p deb-package/usr/share/icons/hicolor/16x16
mkdir -p deb-package/usr/share/icons/hicolor/16x16/apps
mkdir -p deb-package/usr/share/icons/hicolor/22x22
mkdir -p deb-package/usr/share/icons/hicolor/22x22/apps
mkdir -p deb-package/usr/share/icons/hicolor/24x24
mkdir -p deb-package/usr/share/icons/hicolor/24x24/apps
mkdir -p deb-package/usr/share/icons/hicolor/32x32
mkdir -p deb-package/usr/share/icons/hicolor/32x32/apps
mkdir -p deb-package/usr/share/icons/hicolor/48x48
mkdir -p deb-package/usr/share/icons/hicolor/48x48/apps
mkdir -p deb-package/usr/share/kde4
mkdir -p deb-package/usr/share/kde4/services

cp -a ../documentations/* deb-package/usr/share/doc/kakola/
rm -f deb-package/etc/kakola/profiles/*
cp ../profiles/linux/* deb-package/etc/kakola/profiles/

cp ../src/kakola "deb-package/usr/bin/kakola"
mkdir -p "deb-package/usr/share/doc/kakola/translate/"
cp ../src/kakola_*.qm "deb-package/usr/share/doc/kakola/translate/"

chmod 755 deb-package/DEBIAN/postinst
chmod 755 deb-package/DEBIAN/prerm

mkdir ../build/
dpkg -b deb-package ../build/kakola-$ARCH-$VERSION.deb

echo
echo "sudo apt-get install ffmpeg mencoder mplayer mkvtoolnix libqt4-core libqt4-gui"
echo "sudo dpkg -i ../build/kakola-$ARCH-$VERSION.deb"
echo
